package com.cby.android.sample.activities

import android.text.TextUtils
import android.widget.Button
import android.widget.ImageView
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.cby.android.library.base.BaseStateActivity
import com.cby.android.library.base.callback.EmptyCallback
import com.cby.android.library.base.callback.ErrorCallback
import com.cby.android.library.base.callback.LottieLoadingCallback
import com.cby.android.library.base.callback.TimeoutCallback
import com.cby.android.library.ext.observe
import com.cby.android.library.utils.KeyboardUtils
import com.cby.android.library.widget.edittext.MClearEditText
import com.cby.android.library.widget.snackbar.MySnackbar
import com.cby.android.sample.R
import com.cby.android.sample.intent.RollMoreIntent
import com.cby.android.sample.model.RollMoreViewModel
import com.cby.android.sample.state.RollMoreState
import com.kingja.loadsir.core.LoadSir
import kotlinx.coroutines.launch

/**
 * @description: RollQRCodeActivity
 *
 * @author ChenBaoyang
 * @date 4/25/21 20:51
 */
class RollQRCodeActivity : BaseStateActivity<RollMoreViewModel, RollMoreState, RollMoreIntent>() {

    private lateinit var queryBtn: Button
    private lateinit var editText: MClearEditText
    private lateinit var qrcodeIv: ImageView

    override fun bindLiveData() {
        observe(viewModel!!.state) {
            val options = RequestOptions().apply {
                fitCenter()
                placeholder(R.drawable.pic_gray_bg)
            }
            Glide.with(this@RollQRCodeActivity)
                .load(it.dataQRCode?.qrCodeUrl)
                .apply(options)
                .into(qrcodeIv)
        }
    }

    override fun onViewInit() {
        title = "二维码生成"
        queryBtn = findViewById(R.id.btn_query)
        editText = findViewById(R.id.edit_text)
        qrcodeIv = findViewById(R.id.iv_qr_code)

        queryBtn.setOnClickListener {
            KeyboardUtils.hideInputKeyboard(this)

            val content: String = editText.getText().toString()

            if (TextUtils.isEmpty(content)) {
                MySnackbar.makeSnackBarRed(toolbar, "内容不能为空")
                return@setOnClickListener
            }
            lifecycleScope.launch {
                viewModel!!.intents.send(RollMoreIntent.LoadRollQRCodeIntent(content))
            }
        }
    }

    override fun initLoadSir() {
        val loadSir = LoadSir.Builder()
            .addCallback(LottieLoadingCallback())
            .addCallback(EmptyCallback())
            .addCallback(ErrorCallback())
            .addCallback(TimeoutCallback())
            .setDefaultCallback(EmptyCallback::class.java)
            .build()
        loadService = loadSir.register(getTargetView()){
            reload()
        }
    }

    override fun reload() {

    }

    override fun getLayoutId() = R.layout.roll_activity_qr_code

    override fun getTargetView() = qrcodeIv
}